/**
 * COPYRIGHT (C) 2008 Erica Margetich and Loc Phan 
 * To create a class that shuffles a message 
 * @author Erica Margetich
 * @author Loc Phan
 * @version 1.01 2008-12-07
 */

package edu.sjsu.cs.security;

public class ShuffleCipher implements MessageEncoder, MessageDecoder
{
   /**
    * Constructs a Shuffle Cipher
    * @param n the amount of times the message shuffles
    */
   public ShuffleCipher(int n)
   {
      if (n>Integer.MAX_VALUE || n<0) throw new IllegalArgumentException("Illegal Argument");
      else
      {
         times = n;
      }
   }
   /**
    * Returns the encoded message
    * @param message the message to be encoded
    * @return message the message after it is shuffled
    */
   public String encode(String message)
   {
      for(int i = 0;i<times;i++)
      {
         message = doEncode(message);
      }
      return message;
   }
   /**
    * Returns the decoded message
    * @param message the message to be decoded
    * @return message the message after it is shuffled
    */
   public String decode(String message)
   {
      for (int i=0;i<times;i++)
      {
         message = doDecode(message);
      }
     return message;
   }

   /**
    * Encodes the string
    * @param samplestring the string to encode
    * @return the string after it is shuffled
    */
   private String doEncode(String samplestring)
   {
      int length = samplestring.length();
      int half_length = length/2;
      boolean MissingLastChar = false;
      int distance = half_length;
      
      if(length%2==1)
      {
         MissingLastChar = true;
         distance++;
      }
      
      char original_array[] = samplestring.toCharArray();
      char result_array[] = new char[length];
      
      int even_count = 0;
      int odd_count = 1;
      int count = 0;
      
      while (count<half_length)
      {
         result_array[even_count] = original_array[count];
         result_array[odd_count] = original_array[count+distance];
         
         count++;
         even_count+=2;
         odd_count+=2;
      }
      
      if(MissingLastChar)
      {
         result_array[length-1] = original_array[half_length]; 
      }
      
      return new String(result_array);
   }
   /**
    * Decodes the string
    * @param cipherText the string to decode
    * @return cipherText the string after it is shuffled
    */
   private String doDecode(String cipherText)
   {
      int half = cipherText.length()/2;
         String begin = "";
         String end = "";
         if(cipherText.length() % 2 == 0)
         {
            for(int i=0; i<= half + 2; i=i+2)
            {
               begin += cipherText.charAt(i);
               end += cipherText.charAt(i+1);
            }    
            cipherText = begin + end;
         }
         else
         {
            for(int i=0; i< cipherText.length(); i=i+2)
            {
               begin += cipherText.charAt(i);
               
               if (i != cipherText.length() - 1)
                  end += cipherText.charAt(i+1);
            }
            cipherText = begin + end;
         }

      return cipherText; 
   }
   
   //private instance field
   private int times = 0;
}